/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using OpenServers.Net.FtpServer.State;

namespace OpenServers.Net.FtpServer.Command {
    /// <summary>
    ///     <para>
    ///         <b>USER NAME (USER)</b><br/>
    ///         The argument field is a Telnet string identifying the user.
    ///         The user identification is that which is required by the
    ///         server for access to its file system.  This command will
    ///         normally be the first command transmitted by the user after
    ///         the control connections are made (some servers may require
    ///         this).  Additional identification information in the form of
    ///         a password and/or an account command may also be required by
    ///         some servers.  Servers may allow a new USER command to be
    ///         entered at any point in order to change the access control
    ///         and/or accounting information.  This has the effect of
    ///         flushing any user, password, and account information already
    ///         supplied and beginning the login sequence again.  All
    ///         transfer parameters are unchanged and any file transfer in
    ///         progress is completed under the old access control
    ///         parameters.
    ///     </para>
    ///     <para>
    ///         <b>Format</b><br/>
    ///         <code>USER &lt;SP&gt; &lt;username&gt; &lt;CRLF&gt;</code>
    ///     </para>
    ///     <para>
    ///         <b>Success:</b>
    ///         <list type="bullet">
    ///             <item><term>230</term><description>User logged in, proceed.</description></item>
    ///             <item><term>331</term><description>User name okay, need password.</description></item>
    ///             <item><term>332</term><description>Need account for login.</description></item>
    ///         </list>
    ///     </para>
    ///     <para>
    ///         <b>Errors:</b>
    ///         <list type="bullet">
    ///             <item><term>421</term><description>Service not available, closing control connection.</description></item>
    ///             <item><term>500</term><description>Syntax error, command unrecognized.</description></item>
    ///             <item><term>501</term><description>Syntax error in parameters or arguments.</description></item>
    ///             <item><term>530</term><description>Not logged in.</description></item>
    ///         </list>
    ///     </para>
    /// </summary>
    public class UserCommand : BaseCommand {

        /// <summary>
        /// Overrides the base functionality because a user doesn't have to be logged in.
        /// </summary>
        public override void Invoke(IFtpServer server, FtpState state, string arguments) {
            PerformCommand(server, state, arguments);
        }

        /// <summary>
        /// Sets the user name in the state object.
        /// </summary>
        public override void PerformCommand(IFtpServer server, FtpState state, string arguments) {
            if (string.IsNullOrEmpty(arguments)) {
                state.Listener.Send(state, server.Messages.Get501());
            } else {
                state.UserName = arguments;
                state.Listener.Send(state, server.Messages.Get331());
            }
        }
    }
}
